package cn.stylefeng.guns.modular.fi.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicCarrier;
import cn.stylefeng.guns.modular.basic.entity.BasicOperate;
import cn.stylefeng.guns.modular.basic.model.params.BasicCarrierParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicCarrierResult;
import cn.stylefeng.guns.modular.basic.service.BasicCarrierService;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.fi.entity.FiCarrierPma;
import cn.stylefeng.guns.modular.fi.model.params.FiCarrierPmaParam;
import cn.stylefeng.guns.modular.fi.service.FiCarrierPmaService;
import cn.stylefeng.guns.sys.modular.system.entity.Role;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.RoleService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.tools.Tool;
import java.util.Date;


/**
 * 承运商绩效控制器
 *
 * @author zhangzh
 * @Date 2024-11-14 09:18:17
 */
@Controller
@RequestMapping("/fiCarrierPma")
public class FiCarrierPmaController extends BaseController {

    private String PREFIX = "/fi/fiCarrierPma";

    @Autowired
    private FiCarrierPmaService fiCarrierPmaService;

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private BasicOperateService basicOperateService;

    @Autowired
    private BasicCarrierService basicCarrierService;

    /**
     * 跳转到主页面
     *
     * @author zhangzh
     * @Date 2024-11-14
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/fiCarrierPma.html";
    }

    /**
     * 新增页面
     *
     * @author zhangzh
     * @Date 2024-11-14
     */
    @RequestMapping("/add")
    public String add(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));
        return PREFIX + "/fiCarrierPma_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhangzh
     * @Date 2024-11-14
     */
    @RequestMapping("/edit")
    public String edit(Model model,Long id) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));
        FiCarrierPma fiCarrierPma = this.fiCarrierPmaService.getById(id);
        if(fiCarrierPma !=null && ToolUtil.isNotEmpty(fiCarrierPma.getCarrierId())){
            BasicCarrierParam basicCarrierParam = new BasicCarrierParam();
            model.addAttribute("basicCarrierlist", this.basicCarrierService.findListBySpec(basicCarrierParam, fiCarrierPma.getCarrierId()));
        }

        return PREFIX + "/fiCarrierPma_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhangzh
     * @Date 2024-11-14
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(FiCarrierPmaParam fiCarrierPmaParam) {
        User user = userService.getcurrentUser();
        BasicOperate basicOperate = basicOperateService.getById(fiCarrierPmaParam.getOperateId());
        if(basicOperate != null){
            fiCarrierPmaParam.setOperateName(basicOperate.getOperateName());
        }
        BasicCarrier basicCarrier = basicCarrierService.getById(fiCarrierPmaParam.getCarrierId());
        if(basicCarrier != null){
            fiCarrierPmaParam.setCarrierName(basicCarrier.getCarrierName());
        }
        fiCarrierPmaParam.setCreateName(user.getName());
        fiCarrierPmaParam.setCreateTime(new Date());
        fiCarrierPmaParam.setDeptId(user.getDeptId());
        this.fiCarrierPmaService.add(fiCarrierPmaParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhangzh
     * @Date 2024-11-14
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(FiCarrierPmaParam fiCarrierPmaParam) {
        User user = userService.getcurrentUser();
        BasicOperate basicOperate = basicOperateService.getById(fiCarrierPmaParam.getOperateId());
        if(basicOperate != null){
            fiCarrierPmaParam.setOperateName(basicOperate.getOperateName());
        }
        BasicCarrier basicCarrier = basicCarrierService.getById(fiCarrierPmaParam.getCarrierId());
        if(basicCarrier != null){
            fiCarrierPmaParam.setCarrierName(basicCarrier.getCarrierName());
        }
        fiCarrierPmaParam.setUpdateName(user.getName());
        fiCarrierPmaParam.setUpdateTime(new Date());
        this.fiCarrierPmaService.update(fiCarrierPmaParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhangzh
     * @Date 2024-11-14
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(FiCarrierPmaParam fiCarrierPmaParam) {
        this.fiCarrierPmaService.delete(fiCarrierPmaParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhangzh
     * @Date 2024-11-14
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(FiCarrierPmaParam fiCarrierPmaParam) {
        FiCarrierPma detail = this.fiCarrierPmaService.getById(fiCarrierPmaParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author zhangzh
     * @Date 2024-11-14
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(FiCarrierPmaParam fiCarrierPmaParam,String condition ) {
        User user = userService.getcurrentUser();
        String roleId = user.getRoleId();
        if (ToolUtil.isNotEmpty(roleId)) {
            for (String rId : roleId.split(",")) {
                Role byId = roleService.getById(rId);
                if (byId != null && byId.getName().contains("承运商")) {
                    BasicCarrierParam param = new BasicCarrierParam();
                    param.setUserId(user.getUserId());
                    BasicCarrierResult bySpec = basicCarrierService.findBySpec(param, user.getOperateId());
                    if (bySpec != null) {
                        fiCarrierPmaParam.setCarrierId(Long.valueOf(bySpec.getId()));
                    }
                }
            }
        }
        return this.fiCarrierPmaService.findPageBySpec(fiCarrierPmaParam,condition);
    }

}


